<template>
    <div v-loading.fullscreen.lock="isLoading" element-loading-text="loading...">
        <div class="sectiond_1" v-loading.fullscreen.lock="isLoading" element-loading-text="加载中">
            <el-header style="height: 100px;">
                <div class="tit">
                    <el-logo></el-logo>
                    <div class="right">
                        <img src="../../assets/images/login/phone.png" alt="">
                        <span>{{ tel }}</span>
                    </div>
                </div>
            </el-header>
            <el-main class="main">
                <div class="con">
                    <div class="pinfo">
                        <div class="titinfo">更改密码</div>
                        <div class="list">
                            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                                class="demo-ruleForm">
                                <el-form-item label="账户手机号" prop="mobile">
                                    <el-input type="tel" v-model="ruleForm.mobile" placeholder="请输入手机号"
                                        @input="validateForm" maxlength="11" minlength="11"></el-input>
                                </el-form-item>
                                <el-form-item label="手机验证码" prop="mobile_code">
                                    <el-input v-model="ruleForm.mobile_code" placeholder="请输入验证码"
                                        @input="validateForm"></el-input>
                                </el-form-item>
                                <el-form-item label="修改密码" prop="password">
                                    <el-input v-model="ruleForm.password" placeholder="请输入密码"
                                        @input="validateForm"></el-input>
                                </el-form-item>
                                <el-form-item label="确认新密码" prop="againpsd">
                                    <el-input v-model="ruleForm.againpsd" placeholder="请输入密码"
                                        @input="validateForm"></el-input>
                                </el-form-item>
                            </el-form>
                            <button class="send" @click="sendcode" ref="sendBtn"
                                :class="{ 'disabled': isSending, 'send': countdown > 0 }">
                                {{ isSending && countdown > 0 ? `重新发送${countdown}` : '发送验证码' }}
                            </button>
                            <button :disabled="isDisabled" :class="{ 'disabled-button': isDisabled, 'button': true }"
                                @click="onSubmit()">
                                提交
                            </button>

                        </div>
                    </div>
                </div>
            </el-main>
        </div>
        <el-footerss></el-footerss>
    </div>
</template>
  
<script>
import elLogo from "../../common/elLogo.vue";
import elFooterss from "../../common/elFooterss.vue";
import { mapState } from 'vuex';
import { smsUrl } from '../../utils/baseUrl.js'

export default {
    data() {
        // 手机号校验
        var validateMobile = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入手机号'));
            } else if (!/^\d{11}$/.test(value)) {
                callback(new Error('请输入有效的手机号'));
            } else {
                callback();
            }
        };
        // 密码校验
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                callback();
            }
        };
        //   再次输入密码校验
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (this.ruleForm.password !== value) {
                callback(new Error('两次输入的密码不一致！'));
            } else {
                callback();
            }
        };
        return {
            index_base: [],
            isSending: false, // 发送验证码  
            countdown: 60, // 倒计时秒数  
            timer: null, // 定时器 
            ruleForm: {
                mobile: '',
                password: '',
                againpsd: '',
                mobile_code: '',
                scene: 'reset_pass'
            },
            isLoading: false,
            rules: {
                mobile: [
                    { required: true, validator: validateMobile, trigger: 'blur' }
                ],
                password: [
                    { required: true, validator: validatePass, trigger: 'blur' }
                ],
                againpsd: [
                    { required: true, validator: validatePass2, trigger: 'blur' }
                ],
                mobile_code: [
                    { required: true, message: '请输入验证码', trigger: 'blur' },
                    { min: 6, message: '长度不少于 6 个字符', trigger: 'blur' }
                ],
            },
            zp_agreement: [],
            isDisabled: true, // 注册按钮初始禁用状态  
            tel: '',
            isLoading: false,
            is_comp: '',
        };
    },
    async created() {
        const cachedDataStr = localStorage.getItem("zp_index_base");
        if (cachedDataStr) {
            const cachedData = JSON.parse(cachedDataStr);
            this.tel = cachedData.IndexBase.tel
        }
        this.is_comp = this.$route.query.is_comp;
    },
    methods: {
        handleScroll(e) {
            const { scrollTop, clientHeight, scrollHeight } = e.target;
        },
        handleScroll1(e) {
            const { scrollTop, clientHeight, scrollHeight } = e.target;
        },


        checkPrivacy() {
            this.checkFlag = !this.checkFlag;
            console.log("check", this.checkFlag);
            this.validateForm(); // 触发表单验证 
        },
        // 表单验证  
        validateForm() {
            this.$refs.ruleForm.validate((valid) => {
                this.isDisabled = !(valid);
            });
        },
        //获取验证码
        async sendcode() {
            if (this.isSending) {
                return;
            }
            const res = await this.$service.post({
                url: smsUrl,
                data: {
                    mobile: this.ruleForm.mobile,
                    scene: 'reset_pass'
                }
            })
            if (res.code == 1) {
                this.$message.success(res.msg);
                this.startCountdown();
            }
            if (res.code != -1 && res.code != 1) {
                this.$message(res.msg);
            }
        },
        //发送验证码倒计时
        startCountdown() {
            this.isSending = true;
            this.countdown = 60;
            const btn = this.$refs.sendBtn;
            this.timer = setInterval(() => {
                if (this.countdown <= 0) {
                    this.stopCountdown();
                } else {
                    this.countdown--;
                }
            }, 1000)
        },
        stopCountdown() {
            this.isSending = false;
            this.countdown = 0;
            clearInterval(this.timer);
            this.timer = null;
        },
        async onSubmit() {
            this.isLoading = true
            const res = await this.$service.post({
                url: '/api/zp/v1/app_forget_password',
                data: {
                    mobile: this.ruleForm.mobile,
                    password: this.ruleForm.password,
                    againpsd: this.ruleForm.againpsd,
                    mobile_code: this.ruleForm.mobile_code,
                    is_comp: this.is_comp,
                }
            })
            if (res.code == 1) {
                localStorage.setItem("token", res.info.token);
                this.$cache.setCache("username", res.info.username);
                localStorage.removeItem('token');
                setTimeout(() => {
                    this.isLoading = false
                }, 500);
                this.$router.push("/login")
            }
            if (res.code != -1 && res.code != 1) {
                this.$message(res.msg);
                setTimeout(() => {
                    this.isLoading = false
                }, 1000);
            }

        }
    },
    components: {
        elLogo,
        elFooterss
    },
};
</script>
  
<style scoped>
* {
    background: #f3f3f3;
}

button.confirm {
    width: 66px !important;
    height: 30px !important;
    background: #d00 !important;
    position: relative !important;
    font-size: 16px !important;
    border: none !important;
    color: #fff !important;
    position: relative !important;
    top: 11px !important;
    left: 440px !important;
}

.alert {
    background-color: rgba(0, 0, 0, 0.45);
    bottom: 0;
    height: 100%;
    left: 0;
    position: fixed;
    right: 0;
    top: 0;
    z-index: 1000;
    display: flex;
    justify-content: center;
}

.alert .title {
    height: 55px;
    word-wrap: break-word;
    color: rgba(0, 0, 0, 0.85);
    font-size: 18px;
    font-weight: 500;
    margin: 0;
    display: block;
    padding: 0 24px;
    box-sizing: border-box;
    line-height: 55px;
    border: none;
}

.alert .alertbox {
    width: 520px;
    /* height: 850px; */
    height: 90%;
    margin: 50px 0;
    background-color: #fff;
    border-radius: 2px;
}

.alert .alertbox .content {
    width: 500px;
    height: 77%;
    background-color: #fff;
    overflow: auto;
    padding: 24px 0 24px 24px;
    box-sizing: border-box;
    overflow-x: hidden;
}

::v-deep .alert .alertbox .content * {
    background-color: #fff;
}

.alert .alertbox .line1 {
    width: 520px;
    height: 1px;
    background-color: #f3f3f3;
    margin-bottom: 20px;
}

.alert .alertbox .line2 {
    width: 520px;
    height: 1px;
    background-color: #f3f3f3;
    margin-top: 20px;
}

::v-deep .alertbox .el-button {
    width: 66px;
    height: 30px;
    padding: 0;
    background: #f5f5f5;
    border-color: #d9d9d9;
    line-height: 30px;
    position: relative;
    top: 11px;
    left: 440px;
}

.checkBtnInfo {
    width: 281px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 13px;
    color: #999999;
    line-height: 20px;
    text-align: left;
    font-style: normal;
    margin-left: 116px;
    position: relative;
    margin-bottom: 22px;
    background-color: #fff;
}

.checkBtnInfo span {
    color: #496cb3;
    background-color: #fff;
}

input.inputcheck1 {
    position: absolute;
    top: 1px;
    left: -20px;
}

.el-header {
    width: 100%;
    background-color: #fff;
    height: 100px;
    padding: 0;
}

.tit {
    background-color: #fff;
    width: 1200px;
    height: 100px;
    margin: 0 auto;
    padding: 20px 0;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    position: relative;
}

.left {
    height: 60px;
    width: 210px;
    display: flex;
    align-items: center;
    background-color: #fff;
}

.left div {
    background-color: #fff;
}

::v-deep .imagee_1 {
    width: 220px;
    height: 62px;
    background: #fff;
    position: absolute;
    left: 0;
    top: 0;
}

.text div:nth-child(2) {
    font-size: 7px;
}

.el-main {
    /* height: 100vh; */
    background-color: #f3f3f3;
}

.text i {
    font-weight: 600;
    background-color: #fff;
}

.right {
    width: 170px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #fff;
}

::v-deep .el-input__inner {
    width: 300px;
    height: 40px;
}

.right img {
    width: 20px;
    height: 20px;
    background-color: #fff;
}

.right span {
    background-color: #fff;
}

.con {
    margin: 30px auto;
    margin-bottom: 0;
    display: flex;
    justify-content: center;
}

.pinfo {
    width: 800px;
    height: 460px;
    background-color: #fff;
    padding: 20px;
    box-sizing: border-box;
}

.pinfo .titinfo {
    width: 100%;
    margin-top: 10px;
    padding-bottom: 10px;
    border-bottom: 1px solid #DCDFE6;
    background-color: #fff;
    font-weight: 600;
    font-size: 18px;
}

.list {
    width: 400px;
    height: 350px;
    margin: 0 auto;
    margin-top: 25px;
    background-color: #fff;
}

::v-deep .el-form-item__label,
.el-form,
.el-form-item,
::v-deep .el-form-item__error {
    background-color: #fff;
}

button.send {
    height: 25px;
    background-color: #FCE9E8;
    border: none;
    position: relative;
    top: -189px;
    left: 315px;
    color: #FF0000;
    line-height: 25px;
    padding: 0 5px;
    box-sizing: border-box;
}

.button {
    width: 300px;
    height: 40px;
    background-color: #dd0000;
    border-radius: 4px;
    border: none;
    position: relative;
    top: 0px;
    left: 15px;
    color: #fff;
}

/* button:last-child {
    width: 300px;
    height: 40px;
    background-color: #dd0000;
    border-radius: 4px;
    border: none;
    position: relative;
    top: 0px;
    left: 15px;
    color: #fff;
  } */
.disabled-button {
    background-color: #ccc;
    cursor: not-allowed;
}

::v-deep .boxx_33,
::v-deep .boxx_34,
::v-deep .textt_43,
::v-deep .textt_44,
::v-deep .textt_46,
::v-deep .textt_47,
::v-deep .textt_49,
::v-deep .textt_50 {
    display: none !important;
}

::v-deep .groupp_43 {
    justify-content: space-between;
    margin-top: 50px;
}

::v-deep .groupp_44 {
    margin-top: 20px;
}



.send.disabled {
    cursor: not-allowed;
    opacity: 0.6;
    background: #f3f3f3;
}

.create {
    background-color: #d90403;
    color: #fff;
    border-radius: 0px;
    width: 150px;
    margin-top: 35px;
    margin-right: 13px;

}

.cancel {
    background-color: #fff;
    border-radius: 0px;
    width: 150px;
}

.but {
    background-color: #fff;
    margin-left: 69px;
    margin-top: -15px;
}

.main {
    padding: 0;
    width: 100%;

}
</style>